from urllib.parse import urljoin
from bs4 import BeautifulSoup


class HtmlParser(object):
    # def __init__(self):
    #     self.soup=null
    def parse(self, url, html_cont):
        if url is None or html_cont is None:
            return
        soup = BeautifulSoup(html_cont,"html.parser")
        new_urls = self._get_new_urls(url, soup)
        new_data = self._get_new_data(url, soup)
        image_nodes = self.get_image_nodes(soup)
        return new_urls,new_data,image_nodes

    def _get_new_urls(self, url, soup):
        new_urls = set()
        links = soup.select(".cell.type-list.first-cell li a")
        for link in links:
            url_path = link["href"]
            new_url = urljoin(url, url_path)
            # print(new_url)
            new_urls.add(new_url)
        return new_urls

    def _get_new_data(self, url, soup):
        data = {"url": url}
        titles = soup.select(".pic h3")
        data["titles"] = titles
        for title in data["titles"]:
            data["title"] = title.string
            # print(data["title"])
        data['img_nodes'] = self.get_image_nodes(soup)
        for img_node in data['img_nodes']:
            data["img_node"] = img_node
            # print(data["img_node"])
        return data

    def get_img_urls(self, soup):
        img_urls=[]
        image_nodes = self.get_image_nodes(soup)
        for img_node in image_nodes:
           img_url = img_node.atrr["src"]
           img_urls.append(img_url)
        return img_urls

    # 获取img节点
    def get_image_nodes(self,soup):
        img_image_nodes = soup.select(".pic .imgItem")
        return img_image_nodes

